﻿//PlazaTableListComboBox.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Plaza.Xaml
{
	using System.Collections.Specialized;
	using System.Linq;
	using System.Windows;
	using System.Collections.Generic;

	using Ecng.Collections;
	using Ecng.Xaml;

	using StockSharp.Plaza.Metadata;

	using CheckComboBox = Xceed.Wpf.Toolkit.CheckComboBox;

	/// <summary>
	/// Выпадающий список для выбора набора таблиц.
	/// </summary>
	public class PlazaTableListComboBox : CheckComboBox
	{
		static PlazaTableListComboBox()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(PlazaTableListComboBox), new FrameworkPropertyMetadata(typeof(PlazaTableListComboBox)));
		}

		private readonly UniqueObservableCollection<string> _tables = new UniqueObservableCollection<string>();

		/// <summary>
		/// Набор доступных таблиц.
		/// </summary>
		public ICollection<string> Tables { get { return _tables; } }

		/// <summary>
		/// Список идентификаторов выбранных таблиц.
		/// </summary>
		public static readonly DependencyProperty SelectedTablesProperty = DependencyProperty.Register("SelectedTables", typeof(UniqueObservableCollection<string>),
			typeof(PlazaTableListComboBox), new UIPropertyMetadata(SelectedTablesChanged));

		private static void SelectedTablesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var ctrl = d as PlazaTableListComboBox;
			if (ctrl == null)
				return;

			var oldList = (UniqueObservableCollection<string>)e.OldValue;
			if (oldList != null)
				oldList.CollectionChanged -= ctrl.CollectionChanged;

			var list = (UniqueObservableCollection<string>)e.NewValue;
			list.CollectionChanged += ctrl.CollectionChanged;

			ctrl.SelectedItemsOverride = list;
			ctrl.UpdateText();
		}

		private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			UpdateText();
		}

		/// <summary>
		/// Список выбранных таблиц.
		/// </summary>
		public UniqueObservableCollection<string> SelectedTables
		{
			get { return (UniqueObservableCollection<string>)GetValue(SelectedTablesProperty); }
			set { SetValue(SelectedTablesProperty, value); }
		}

		/// <summary>
		/// Создать <see cref="PlazaTableListComboBox"/>.
		/// </summary>
		public PlazaTableListComboBox()
		{
			var registry = new PlazaTableRegistry();
			
			Tables.AddRange(registry.AllTables.Select(t => t.Id));
			ItemsSource = _tables;

			SelectedTables = new UniqueObservableCollection<string>();
			SelectedTables.AddRange(registry.DefaultTables.Select(t => t.Id));
		}

		/// <summary>
		/// Метод, который вызывается при изменении выбранного значения.
		/// </summary>
		/// <param name="oldValue">Старое значение.</param>
		/// <param name="newValue">Новое значение.</param>
		protected override void OnSelectedValueChanged(string oldValue, string newValue)
		{
			base.OnSelectedValueChanged(oldValue, newValue);
			UpdateText();
		}

		private void UpdateText()
		{
			Text = "Выбрано: " + (SelectedTables != null ? SelectedTables.Count : 0);
		}
	}
}